package com.jmpt.fangbohui.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.jpush.android.api.JPushInterface;

import com.jmpt.fangbohui.MyApplication;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.ui.base.BaseActivity;
import com.jmpt.fangbohui.ui.fragment.MainHomeFragment;
import com.jmpt.fangbohui.ui.fragment.MainInfoFragment;
import com.jmpt.fangbohui.ui.fragment.MainLocalFragment;
import com.jmpt.fangbohui.ui.fragment.MainMoreFragment;
import com.jmpt.fangbohui.ui.fragment.MainUserFragment;
import com.jmpt.fangbohui.utils.AppUtils;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.view.CustomViewPager;
import com.jmpt.fangbohui.view.LazyViewPager.OnPageChangeListener;
import com.jmpt.fangbohui.view.NoScrollViewPager;


public class MainActivity extends BaseActivity {

    private LinearLayout linContent;
	private CustomViewPager vpContent;
    private RadioGroup mainRadio;
    private RadioButton rb_home;
    private RadioButton rb_info;
    private RadioButton rb_user;
    private RadioButton rb_local;
    private RadioButton rb_more;
    private ImageView loading;

    private LayoutInflater mLayoutInflater;
    private List<Fragment> tabFragments;
    private List<RadioButton> radioButtons;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        MyApplication.initImageLoader(this);
        mContext = this;
        setContentView(R.layout.activity_main);

        mLayoutInflater = LayoutInflater.from(this);
        tabFragments = new ArrayList<Fragment>();
        radioButtons = new ArrayList<RadioButton>();
        assignViews();
        initView();
    }

    public void hideLoading() {
        loading.setVisibility(View.GONE);

    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);

        int versionCode = AppUtils.getVersionCode(mContext);
//        if (MyApplication.getSharedPreferences().getInt(Constants.SharedPreferences.VERSION_CODE, 0) == 0) {
//            MyApplication.getSharedPreferences().edit().putInt(Constants.SharedPreferences.VERSION_CODE, versionCode).commit();
//            Intent intent = new Intent(mContext, FirstLoadingGuideActivity.class);
//            startActivity(intent);
//        } else {
//            if (LocationHelper.getRegion() == null) {
//                Intent intent = new Intent(mContext, LocationActivity.class);
//                startActivity(intent);
//            }
//        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        try {
//            tabFragments.get(3).onActivityResult(requestCode, resultCode, data);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
    
    private void assignViews() {
        linContent = (LinearLayout) findViewById(R.id.lin_main_Content);
        vpContent = (CustomViewPager) findViewById(R.id.pagerContent);
        mainRadio = (RadioGroup) findViewById(R.id.rg_main);
        rb_home = (RadioButton) findViewById(R.id.rb_home);
        rb_info = (RadioButton) findViewById(R.id.rb_info);
        rb_user = (RadioButton) findViewById(R.id.rb_user);
        rb_local = (RadioButton) findViewById(R.id.rb_local);
        rb_more = (RadioButton) findViewById(R.id.rb_more);
    }

    private void initView() {
//        tabFragments.add(new MainHomeFragment());
//        tabFragments.add(new MainInfoFragment());
//        tabFragments.add(new MainUserFragment());
//        tabFragments.add(new MainLocalFragment());
//        tabFragments.add(new MainMoreFragment());
        radioButtons.add(rb_home);
        radioButtons.add(rb_info);
        radioButtons.add(rb_user);
        radioButtons.add(rb_local);
        radioButtons.add(rb_more);
        vpContent.setOffscreenPageLimit(0);
        //设置viewpage不可滑动
        vpContent.setPagingEnabled(false);
        vpContent.setAdapter(new HomeMainPagerAdapter(getSupportFragmentManager()));
        vpContent.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        mainRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                DebugLog.i("i:" + i);
                int index = 0;
                if (i == rb_home.getId()) {
                    index = 0;
                } else if (i == rb_info.getId()) {
                    index = 1;
                } else if (i == rb_user.getId()) {
                    index = 2;
                } else if (i == rb_local.getId()) {
                    index = 3;
                } else if (i == rb_more.getId()) {
                    index = 0;
                    Intent intent = new Intent(MainActivity.this,BuyOldHouseListActivity.class);
                    startActivity(intent);
                }
                vpContent.setCurrentItem(index, false);
            }

        });
        //打开应用显示首页
        rb_home.setChecked(true);

    }

    /*class HttpHomeHandler extends JsonHttpHandler {

        public HttpHomeHandler(Context context) {
            super(context);
        }

        @Override
        public void onDo(JSONObject response) {
            super.onDo(response);
            HomeBean homeBean = new Gson().fromJson(response.toString(), HomeBean.class);
            MyApplication.getSharedPreferences().edit().putString(Constants.SharedPreferences.HOME_DATA, new Gson().toJson(homeBean)).commit();
            ImageLoaderHelper.getImageLoader().loadImage(homeBean.loading.image, ImageLoaderHelper.getBaseDisplayImageOptions(), null);
        }

        @Override
        public void onStart() {
            super.onStart();

        }

        @Override
        public void onFinish() {
            super.onFinish();
            hideLoading();
            linContent.setVisibility(View.VISIBLE);
            vpContent.setAdapter(new HomeMainPagerAdapter(getSupportFragmentManager()));
        }
    }*/


   


    class HomeMainPagerAdapter extends FragmentPagerAdapter {
    	private Map<Integer,Fragment> tabFragments = new LinkedHashMap<Integer, Fragment>();
        public HomeMainPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
        	Fragment fragment = tabFragments.get(i);
    		if(fragment == null){
    			switch (i) {
    			case 0:
    				fragment = new MainHomeFragment();
    				break;
    			case 1:
    				fragment = new MainInfoFragment();
    				break;
    			case 2:
    				fragment = new MainUserFragment();
    				break;
    			case 3:
    				fragment = new MainLocalFragment();
    				break;
    			case 4:
    				fragment = new MainMoreFragment();
    				break;
    			default:
    				break;
    			}
    			tabFragments.put(i, fragment);
    		}
    		return fragment;
        }

        @Override
        public int getCount() {
            return 5;
        }
    }
    
    @SuppressWarnings("deprecation")
	@Override
    public void onBackPressed() {
    	if(vpContent.getCurrentItem() != 0){
    		vpContent.setCurrentItem(0, false);
    		rb_home.setChecked(true);
    		return;
    	}
    	Builder builder = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = builder.create();
        
        alertDialog.setTitle("提示");
        alertDialog.setMessage("您确认退出客户端吗？");
        alertDialog.setButton("退出", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				finish();
			}
		});
        alertDialog.setButton2("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				alertDialog.dismiss();
			}
		});
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.show();
    }

}

